package com.ruoyi.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.domain.bo.RefundBo;
import com.ruoyi.supplier.domain.vo.RefundVo;
import com.ruoyi.supplier.domain.Refund;
import com.ruoyi.supplier.mapper.RefundMapper;
import com.ruoyi.supplier.service.IRefundService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 售后单记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class RefundServiceImpl implements IRefundService {

    private final RefundMapper baseMapper;

    /**
     * 查询售后单记录
     */
    @Override
    public RefundVo queryById(Integer refundId){
        return baseMapper.selectVoById(refundId);
    }

    /**
     * 查询售后单记录列表
     */
    @Override
    public TableDataInfo<RefundVo> queryPageList(RefundBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Refund> lqw = buildQueryWrapper(bo);
        Page<RefundVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询售后单记录列表
     */
    @Override
    public List<RefundVo> queryList(RefundBo bo) {
        LambdaQueryWrapper<Refund> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Refund> buildQueryWrapper(RefundBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Refund> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSupplierId() != null, Refund::getSupplierId, bo.getSupplierId());
        lqw.eq(bo.getMerchantId() != null, Refund::getMerchantId, bo.getMerchantId());
        lqw.eq(bo.getType() != null, Refund::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getApplyDesc()), Refund::getApplyDesc, bo.getApplyDesc());
        lqw.eq(bo.getIsAgree() != null, Refund::getIsAgree, bo.getIsAgree());
        lqw.eq(StringUtils.isNotBlank(bo.getRefuseDesc()), Refund::getRefuseDesc, bo.getRefuseDesc());
        lqw.eq(bo.getStatus() != null, Refund::getStatus, bo.getStatus());
        lqw.eq(bo.getRefundMoney() != null, Refund::getRefundMoney, bo.getRefundMoney());
        lqw.eq(bo.getIsReceipt() != null, Refund::getIsReceipt, bo.getIsReceipt());
        lqw.eq(StringUtils.isNotBlank(bo.getRefuseReceiptDesc()), Refund::getRefuseReceiptDesc, bo.getRefuseReceiptDesc());
        lqw.eq(bo.getIsRefund() != null, Refund::getIsRefund, bo.getIsRefund());
        return lqw;
    }

    /**
     * 新增售后单记录
     */
    @Override
    public Boolean insertByBo(RefundBo bo) {
        Refund add = BeanUtil.toBean(bo, Refund.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRefundId(add.getRefundId());
        }
        return flag;
    }

    /**
     * 修改售后单记录
     */
    @Override
    public Boolean updateByBo(RefundBo bo) {
        Refund update = BeanUtil.toBean(bo, Refund.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Refund entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除售后单记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
